//
//  LXJWelcomeController.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/29.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

/// 欢迎页面

class LXJWelcomeController: UIViewController {
    
    /// 控件的懒加载
    /// 背景视图
    lazy var backgroundView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像视图
    lazy var headIconView: UIImageView = {
        let imgView = UIImageView()
        
        /// 判断是否有头像
        if let iconURLStr = LXJOAuthViewModel.sharedOAuthViewModel.userAccount?.avatar_large{
            /// 一定有头像,异步加载网络头像
            imgView.sd_setImage(with: URL(string: iconURLStr), placeholderImage: UIImage(named: "avatar_default_big" ))
            
            /// 设置圆角
            imgView.layer.cornerRadius = 45
            imgView.layer.masksToBounds = true
        }
        return imgView
    }()
    
    /// 欢迎消息
    lazy var welcomeMessage: UILabel = {
        let label = UILabel()
        /// 隐藏欢迎消息
        label.alpha = 0
        /// 文字颜色
        label.textColor = UIColor.darkGray
        /// 文字大小
        label.font = UIFont.systemFont(ofSize: 13)
        /// 文字内容
        /// 表示该用户有昵称
        if let username = LXJOAuthViewModel.sharedOAuthViewModel.userAccount?.name{
            label.text = "欢迎回来,\(username)"
        } else{/// 用户没有昵称
            label.text = "欢迎回来"
        }
        /// 自动匹配大小
        label.sizeToFit()
        
        return label
    }()
    
    /// 自定义控制器的视图
    override func loadView() {
        super.loadView()
        view = backgroundView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        /// 界面搭建
        setupUI()
    }
    
    /// 执行头像的弹簧效果
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        /// 执行头像的弹簧效果
        startIconAnimation()
    }
    
    // MARK: 界面搭建
    func setupUI() -> Void {
        /// 修改背景颜色
        view.backgroundColor = UIColor.white
        
        /// 添加控件
        view.addSubview(headIconView)
        view.addSubview(welcomeMessage)
        
        /// 自动布局
        headIconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.centerY.equalTo(self.view).offset(-100)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        welcomeMessage.snp.makeConstraints { (make) in
            make.centerX.equalTo(headIconView)
            make.top.equalTo(headIconView.snp.bottom).offset(10)
        }
    }
    
    /// 头像的弹簧动画
    private func startIconAnimation() -> Void {
        /// 更改头像的约束
        headIconView.snp.updateConstraints { (make) in
            make.centerY.equalTo(self.view).offset(-200)
        }
        
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: [], animations: {
            /// 执行动画
            self.view.layoutIfNeeded()
        }) { (_) in
            ///  结束动画之后弹出欢迎消息
            UIView.animate(withDuration: 1.0, delay: 0, options: [], animations: {
                /// 显示欢迎消息
                self.welcomeMessage.alpha = 1
            }, completion: { (_) in
                /// 全部动画结束之后,发通知切换根视图控制器
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "SwitchRootViewContrlooer"), object: nil)
            })
        }
    }
}
